//
//  PointsManager.m
//  Cunli
//
//  Created by 湛亚 on 15/8/14.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "PointsManager.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "UIImageView+EMWebCache.h"
#import "UIColor+Addition.h"
#import "TongXunLu.h"
#import "TTTAttributedLabel.h"
#import "LinLi.h"

#define screenWidth ([UIScreen mainScreen].bounds.size.width)
#define screenheight ([UIScreen mainScreen].bounds.size.height)
@implementation PointsManager

+ (void)sendPointWithUserid:(NSString *)userUserid andType:(NSString *)type{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:userUserid forKey:@"to_userid"];
    [CheckParams setObject:type forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"app_point_talk.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
    }failureAction:^{
        
    }];
    
}

+ (void)sendSharePointWithChannel:(NSString *)channel andItemid:(NSString *)itemid{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:channel forKey:@"channel"];
    [CheckParams setObject:itemid forKey:@"item_id"];
    [CheckParams setObject:@"24" forKey:@"item_mid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"app_point_share.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
    }failureAction:^{
        
    }];
}

+(TTTAttributedLabel *)createWithframe:(CGRect)frame andString:(NSString *)str{
    
    TTTAttributedLabel *bubbleText = [[TTTAttributedLabel alloc]
                                      initWithFrame:frame];
    bubbleText.backgroundColor = [UIColor clearColor];
    bubbleText.numberOfLines = 0;
    bubbleText.font=[UIFont systemFontOfSize:16];
    bubbleText.textColor=[UIColor convertHexColorToUIColor:0x333333];
    bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
    bubbleText.text = str;
    bubbleText.linkAttributes = @{(NSString *)kCTUnderlineStyleAttributeName : [NSNumber numberWithBool:NO],
                                  (NSString*)kCTForegroundColorAttributeName : (id)[[UIColor convertHexColorToUIColor:0x5fbafd] CGColor]};
    bubbleText.highlightedTextColor = [UIColor whiteColor];
    bubbleText.verticalAlignment = TTTAttributedLabelVerticalAlignmentCenter;
    // 提取出文本中的超链接
    //bubbleText.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    
    NSError *error;
    NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:str
                                                options:0
                                                  range:NSMakeRange(0, [str length])];
    //NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:str];
    for (NSTextCheckingResult *match in arrayOfAllMatches) {
        NSString *substringForMatch = [str substringWithRange:match.range];
        //[attribute addAttribute:(NSString *)kCTFontAttributeName value:(id)bubbleText.font range:match.range];
        //[attribute addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:match.range];
        [bubbleText addLinkToURL:[NSURL URLWithString:substringForMatch] withRange:match.range];
    }
    
    return bubbleText;
}

+(TTTAttributedLabel *)createWithframe:(CGRect)frame andNSMutableAttributedString:(NSMutableAttributedString *)string andNsstring:(NSString *)str{
    
    TTTAttributedLabel *bubbleText = [[TTTAttributedLabel alloc]
                                      initWithFrame:frame];
    bubbleText.backgroundColor = [UIColor clearColor];
    bubbleText.numberOfLines = 0;
    bubbleText.lineBreakMode = NSLineBreakByWordWrapping;
    bubbleText.text = string;
    bubbleText.linkAttributes = @{(NSString *)kCTUnderlineStyleAttributeName : [NSNumber numberWithBool:NO],(NSString*)kCTForegroundColorAttributeName : (id)[[UIColor convertHexColorToUIColor:0x5fbafd] CGColor]};
    bubbleText.highlightedTextColor = [UIColor whiteColor];
    bubbleText.verticalAlignment = 0;
    // 提取出文本中的超链接
    bubbleText.enabledTextCheckingTypes = NSTextCheckingTypeLink;
    
    NSError *error;
    NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:str
                                                options:0
                                                  range:NSMakeRange(0, [str length])];
    //NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:str];
    for (NSTextCheckingResult *match in arrayOfAllMatches) {
        NSString *substringForMatch = [str substringWithRange:match.range];
        //[attribute addAttribute:(NSString *)kCTFontAttributeName value:(id)bubbleText.font range:match.range];
        //[attribute addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:match.range];
        [bubbleText addLinkToURL:[NSURL URLWithString:substringForMatch] withRange:match.range];
    }
    
    return bubbleText;
}

+(float)heightWithString:(NSString *)introduce{
    
    CGRect rect = [introduce boundingRectWithSize:CGSizeMake(screenWidth-70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f]} context:nil];
    CGFloat height = ceilf(rect.size.height);
    return height + 10;
}

+(BOOL)isDefaultTouXiangWithUrlstring:(NSString *)urlString{
    
    NSRange foundObj=[urlString rangeOfString:@"default_touxiao.png" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0) {
        return YES;
    }else{
        return NO;
    }
}

+(void)saveHuanXinUserArrayWithArray:(NSMutableDictionary *)userDic{
    
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"userDic.src"];
    BOOL success = [NSKeyedArchiver archiveRootObject:userDic toFile:filePath];
    if(success){
        NSLog(@"保存成功");
    }
}

+(NSMutableDictionary *)getHuanXinUserInfoDic{
    
    NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"userDic.src"];
    id userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",userDic);
    return userDic;
}

+ (void)delectCommentWithType:(NSString *)type itemid:(NSString *)itemid completionBlock:(RequestBlock)block{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:type forKey:@"type"];
    [CheckParams setObject:itemid forKey:@"itemid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"del_comment.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"del_comment"]) {
            block(YES);
        }else{
            block(NO);
        }
        
    }failureAction:^{
        
    }];
}

#pragma mark -获取所有用户信息环信找人

+(void)getAllHuanXinUserInfo{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"addressbook_galler.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"pointmanager:%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"addressbook_galler"]) {
            
            NSDictionary *dataDic=[result objectForKey:@"data"];
            NSArray *listArray=[dataDic objectForKey:@"list"];
            
            if (listArray.count>0) {
                
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                NSMutableDictionary *huanXinDic=[[NSMutableDictionary alloc]init];
                
                for (NSDictionary *dic in listArray) {
                    
                    TongXunLu *tongXunLu=[TongXunLu yy_modelWithDictionary:dic];
                    [huanXinDic setObject:tongXunLu.truename forKey:[NSString stringWithFormat:@"%@name",tongXunLu.huanxin_username]];
                    [huanXinDic setObject:tongXunLu.pic forKey:[NSString stringWithFormat:@"%@pic",tongXunLu.huanxin_username]];
                    [huanXinDic setObject:[NSString stringWithFormat:@"%d",tongXunLu.userid] forKey:[NSString stringWithFormat:@"%@userid",tongXunLu.huanxin_username]];
                }
                [userDefault setObject:huanXinDic forKey:@"huanXinInfo"];
                [userDefault synchronize];
            }
            
        }
        
    }failureAction:^{
        
    }];
}

+(CGSize)onePicHeightWithWidth:(float)picWidth andHeight:(float)picHeight{
    
    CGSize picSize;
    if (picWidth<ScreenWidth-30&&picHeight<250) {
        picSize.height=picHeight;
        picSize.width=picWidth;
    }else{
        if(picWidth>ScreenWidth-30&&picHeight<250) {
            picSize.height=ceilf((ScreenWidth-30)*picHeight/picWidth);
            picSize.width=ScreenWidth-30;
        }else if(picWidth<ScreenWidth-30&&picHeight>250){
            picSize.height=250;
            picSize.width=ceilf(250*picWidth/picHeight);
        }else if(picWidth>ScreenWidth-30&&picHeight>250){
            picSize.width=ScreenWidth-30;
            picSize.height=ceilf((ScreenWidth-30)*picHeight/picWidth);
        }else{
            float imgWidth=ceilf((ScreenWidth-40)/3.0);
            picSize.width=imgWidth;
            picSize.height=imgWidth;
        }
    }
    return picSize;
}

@end
